﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using enqueteJwVd.DAL;
using enqueteJwVd.Services;
using enqueteJwVd.ViewModels;


namespace enqueteJwVd.Controllers
{
    [Authorize(Roles = "e-manager")]
    public class QuestionTypesController : Controller
    {

        UnitOfWork unitOfWork = new UnitOfWork();
        IQuestionTypeService questionTypeService;
        //private VDLDbEntities db = new VDLDbEntities();

        public QuestionTypesController(IQuestionTypeService questionTypeService)
        {
            this.questionTypeService = questionTypeService;
        }

        public QuestionTypesController()
        {
            
        }

        
        //
        // GET: /QuestionTypes/

        public ViewResult Index()
        {
            var questionTypes = questionTypeService.GetAllQuestionTypes();
            var viewModel = new QuestionTypeIndexViewModel() { QuestionTypes = questionTypes };
            return View(viewModel);
        }

        //
        // GET: /QuestionTypes/Details/5

        public ViewResult Details(int? id)
        {
            QuestionType questiontype = unitOfWork.QuestionTypeRepository.GetById(id);
            return View(questiontype);
        }

        //
        // GET: /QuestionTypes/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /QuestionTypes/Create

        [HttpPost]
        public ActionResult Create([Bind(Exclude = "Id")] QuestionType questiontype)
        {
            if (ModelState.IsValid)
            {
                unitOfWork.QuestionTypeRepository.Add(questiontype);
                unitOfWork.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(questiontype);
        }
        
        //
        // GET: /QuestionTypes/Edit/5
 
        public ActionResult Edit(int? id)
        {
            QuestionType questiontype = unitOfWork.QuestionTypeRepository.GetById(id);
            return View(questiontype);
        }

        //
        // POST: /QuestionTypes/Edit/5

        [HttpPost]
        public ActionResult Edit(QuestionType questiontype)
        {
            if (ModelState.IsValid)
            {
                unitOfWork.QuestionTypeRepository.Update(questiontype);
                unitOfWork.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(questiontype);
        }

        //
        // GET: /QuestionTypes/Delete/5
 
        public ActionResult Delete(int? id)
        {
            QuestionType questiontype = unitOfWork.QuestionTypeRepository.GetById(id);
            return View(questiontype);
        }

        //
        // POST: /QuestionTypes/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int? id)
        {
            QuestionType questiontype = unitOfWork.QuestionTypeRepository.GetById(id);
            unitOfWork.QuestionTypeRepository.Delete(questiontype);
            unitOfWork.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            unitOfWork.Dispose();
            base.Dispose(disposing);
        }
    }
}